package com.cgtong.android.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.callback.EmptyCallback;
import com.cgtong.android.callback.LoadingCallback;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.dyhdyh.widget.loading.factory.MaterialDialogFactory;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * fragment布局带有标题栏，同时初始化时，内容显示loading，需要继承此类
 * （如果初始化时，不需要显示loading,直接在loadDataByNet方法中，调用PostUtil.postCallbackDelayed(loadService, SuccessCallback.class)
 * 或者PostUtil.postSuccessDelayed(loadService),和直接继承BaseFragment相同;）
 * Created by zhuqingchun
 * 2017/9/30 14:50.
 */

public abstract class BaseTitleFragment extends Fragment {

    protected abstract String getContentTitle();

    public abstract int getLayoutId();

    public abstract void initData();

    protected abstract void loadDataByNet();//网络请求处理

    protected abstract void onDataReloadByNet(View v);//再次请求处理

    private Unbinder mUnbinder;
    protected LoadService loadService;

//    public ImageView mBackLeftImg;
    public TextView mTitleLeftTxt;
    public TextView mTitleCenterTxt;
    public TextView mTitleRightTxt;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.include_fragment_title_loading_layout, container, false);
        RelativeLayout titleBarView = (RelativeLayout) rootView.findViewById(R.id.rl_titleBar);
//        mBackLeftImg = (ImageView) rootView.findViewById(R.id.back_left_img);
        mTitleLeftTxt = (TextView) rootView.findViewById(R.id.title_left_txt);
        mTitleCenterTxt = (TextView) rootView.findViewById(R.id.title_center_txt);
        mTitleRightTxt = (TextView) rootView.findViewById(R.id.title_right_txt);

        mTitleCenterTxt.setText(getContentTitle() == null ? "" : getContentTitle());
        FrameLayout contentView = (FrameLayout) rootView.findViewById(R.id.fl_content);
        rootView.removeView(contentView);
        View content = inflater.inflate(getLayoutId(), container, false);
        if (content != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            contentView.addView(content, params);
        }
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
                .setDefaultCallback(LoadingCallback.class)
                .build();
        loadService = loadSir.register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                onDataReloadByNet(v);
            }

        });
        mUnbinder = ButterKnife.bind(this, contentView);
        return loadService.getTitleLoadLayout(getContext(), rootView, titleBarView);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        loadDataByNet();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }


    /**
     * 显示加载框
     */
    public void doShowLoading(Context context, boolean cancelable) {
        this.doShowLoading(context, null, cancelable);
    }

    public void doShowLoading(Context context, String msg, boolean cancelable) {
        LoadingDialog loadingDialog = LoadingDialog.make(context, new MaterialDialogFactory());
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setMessage(msg);//提示消息
        } else {
            loadingDialog.setMessage(getString(R.string.request_net))//提示消息
                    .setCancelable(cancelable);
        }
        loadingDialog.create().show();
    }

    //友盟页面统计
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getContentTitle());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getContentTitle());
    }

    public void doHideLoading() {
        LoadingDialog.cancel();
    }
}